package com.lenovo.smarttraffic.ui.fragment;

import android.animation.ObjectAnimator;
import android.os.CountDownTimer;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.InitApp;
import com.lenovo.smarttraffic.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Random;


/**
 * @author Cyzen
 * @date 2020/10/25
 * description 设计页面
 */
public class DesignFragment extends BaseFragment {
    private static DesignFragment instance = null;
    private android.support.v7.widget.CardView cv1;
    private TextView t1;
    private android.support.v7.widget.CardView cv2;
    private TextView t2;
    private android.support.v7.widget.CardView cv3;
    private TextView t3;
    private com.github.mikephil.charting.charts.RadarChart c1;
    private com.github.mikephil.charting.charts.LineChart c2;
    private com.github.mikephil.charting.charts.PieChart c3;
    private com.github.mikephil.charting.charts.BarChart c4;

    public static DesignFragment getInstance() {
        if (instance == null) {
            instance = new DesignFragment();
        }
        return instance;
    }

    int c[] = {R.color.Indigo, R.color.Blue, R.color.Cyan, R.color.Deep_Orange, R.color.Deep_Purple};
    Random random = new Random();

    @Override
    protected View getSuccessView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.frag_design, null);
        initView(view);

        c1.setWebColor(getResources().getColor(R.color.Green));
        c1.getXAxis().setEnabled(false);
        c1.getYAxis().setEnabled(false);

        c1.getDescription().setEnabled(false);
        c2.getDescription().setEnabled(false);
        c3.getDescription().setEnabled(false);
        c4.getDescription().setEnabled(false);

        c1.getLegend().setEnabled(false);
        c3.getLegend().setEnabled(false);

        c1.setHighlightPerTapEnabled(false);

        cv2.setOnClickListener(v -> r(v, "rotationX"));

        new CountDownTimer(Long.MAX_VALUE, 1) {

            @Override
            public void onTick(long millisUntilFinished) {
                c1.setRotation(c1.getRotation() + 1);
                c3.setRotation(c3.getRotation() + 1);
            }

            @Override
            public void onFinish() {

            }
        }.start();
        new CountDownTimer(Long.MAX_VALUE, 3000) {

            @Override
            public void onTick(long millisUntilFinished) {
                cv1.setCardBackgroundColor(getResources().getColor(c[random.nextInt(c.length - 1)]));
                cv2.setCardBackgroundColor(getResources().getColor(c[random.nextInt(c.length - 1)]));
                cv3.setCardBackgroundColor(getResources().getColor(c[random.nextInt(c.length - 1)]));

                t1.setText("温度：" + random.nextInt(100));
                t2.setText("湿度：" + random.nextInt(100));
                t3.setText("PM2.5：" + random.nextInt(100));

                r(cv1, "rotationY");
                r(cv3, "rotationY");

                ArrayList<RadarEntry> list1 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list1.add(new RadarEntry(random.nextInt(100)));
                }
                RadarDataSet radarDataSet = new RadarDataSet(list1, "");
                radarDataSet.setColor(getResources().getColor(c[random.nextInt(c.length - 1)]));
                c1.setData(new RadarData(radarDataSet));
                c1.invalidate();

                ArrayList<Entry> list2 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list2.add(new Entry(i, random.nextInt(100)));
                }
                LineDataSet lineDataSet = new LineDataSet(list2, "");
                lineDataSet.setColors(c, getContext());
                c2.setData(new LineData(lineDataSet));
                c2.animateXY(2000, 1500, Easing.EasingOption.EaseInOutExpo, Easing.EasingOption.EaseInExpo);

                ArrayList<PieEntry> list3 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list3.add(new PieEntry(random.nextInt(100)));
                }
                PieDataSet pieDataSet = new PieDataSet(list3, "");
                pieDataSet.setColors(c, getContext());
                c3.setData(new PieData(pieDataSet));
                c3.invalidate();

                ArrayList<BarEntry> list4 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list4.add(new BarEntry(i, random.nextInt(100)));
                }
                BarDataSet barDataSet = new BarDataSet(list4, "");
                barDataSet.setColors(c, getContext());
                c4.setData(new BarData(barDataSet));
                c4.animateXY(2000, 1500, Easing.EasingOption.EaseInOutExpo, Easing.EasingOption.EaseInOutExpo);
            }

            @Override
            public void onFinish() {

            }
        }.start();


        return view;
    }

    void r(View v, String s) {
        ObjectAnimator.ofFloat(v, s, 0, 360).setDuration(1500).start();
    }

    @Override
    protected Object requestData() {
        return Constant.STATE_SUCCEED;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onDestroy() {
        if (instance != null) {
            instance = null;
        }
        super.onDestroy();
    }

    private void initView(View view) {
        cv1 = (CardView) view.findViewById(R.id.cv1);
        t1 = (TextView) view.findViewById(R.id.t1);
        cv2 = (CardView) view.findViewById(R.id.cv2);
        t2 = (TextView) view.findViewById(R.id.t2);
        cv3 = (CardView) view.findViewById(R.id.cv3);
        t3 = (TextView) view.findViewById(R.id.t3);
        c1 = (RadarChart) view.findViewById(R.id.c1);
        c2 = (LineChart) view.findViewById(R.id.c2);
        c3 = (PieChart) view.findViewById(R.id.c3);
        c4 = (BarChart) view.findViewById(R.id.c4);
    }
}
